package com.kerbores.blog.module.admin;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.mvc.View;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Attr;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import com.kerbores.blog.bean.Article;
import com.kerbores.blog.bean.Catalog;
import com.kerbores.blog.bean.User;
import com.kerbores.blog.filter.AdminFilter;
import com.kerbores.blog.module.common.BaseModule;
import com.kerbores.blog.service.IArticleService;
import com.kerbores.blog.service.ICatalogService;
import com.kerbores.blog.service.ITagService;
import com.kerbores.utils.entries.Result;

/**
 * @author Ixion <br>
 *         create at 2014年10月25日
 */
@At("admin/article")
@Filters(@By(type = AdminFilter.class))
public class AdminArticleModule extends BaseModule {

	@Inject
	private IArticleService articleService;

	@Inject
	private ITagService tagService;

	@Inject
	private ICatalogService catalogService;

	@At("/create")
	@Ok("vel:admin/article/create.vm")
	public Result create() {
		data.put("acls", Article.ACL.values());
		data.put("tags", tagService.listAll());
		return Result.success(data).setTitle("新建文章");
	}

	@At("/doCreate")
	public View doCreate(@Param("..") Article article, @Attr("loginUser") User user) {
		article.setIp(_ip());
		article.setAuthorId(user.getId());
		boolean flag = articleService.save(article);
		if (flag) {
			return _renderRedirct("/admin/main");
		}
		data.put("acls", Article.ACL.values());
		data.put("tags", tagService.listAll());
		return _renderVelocity("admin/article/create.vm", Result.success(data).setTitle("新建文章"));
	}

	@At("/catalog")
	@Ok("vel:admin/article/catalog.vm")
	public Result catalog() {
		return Result.success().setTitle("文章分组");
	}

	@At("/catalog/add")
	@Ok("vel:admin/article/catalogAdd.vm")
	@GET
	public Result catalogAddForm() {
		return Result.success().setTitle("添加分组");
	}

	@At("/catalog/add")
	@Ok("vel:admin/article/catalogAdd.vm")
	@POST
	public View catalogAdd(@Param("..") Catalog catalog) {
		boolean flag = catalogService.save(catalog);
		if (flag) {
			return _renderRedirct("/admin/article/catalog");
		}
		return _renderVelocity("admin/article/catalogAdd.vm", Result.fail("添加分组失败").setTitle("添加分组"));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.kerbores.blog.module.common.BaseModule#_getNameSpace()
	 */
	@Override
	public String _getNameSpace() {
		return "admin/article";
	}

}
